import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter3demo/common_util.dart';

void main() {
  runApp(MyApp());
  GestureBinding.instance.gestureArena;
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        // appBar: AppBar(
        //   title: Text('Text Width Example'),
        // ),
        body: MyHomePage(title: "1",),
      ),
    );
  }




  ///同时响应
  Widget listenerStack(){
    return SafeArea(
      minimum: EdgeInsets.symmetric(horizontal: 20,vertical: 100),
      child: Listener(
        behavior: HitTestBehavior.opaque,
        onPointerDown: (_){
          print("3333333");
        },
        child: Container(
          padding: EdgeInsets.all(20),
          color: CommonUtil.randomColor(),
          child: Stack(
            // mainAxisAlignment: MainAxisAlignment.end,
            children: [
              ListenerWithDiffrentBehavior(
                printInfo: "11111",
                width: 300,
                behavior: HitTestBehavior.translucent,
              ),
              ListenerWithDiffrentBehavior(
                printInfo: "2222",
                width: 200,
                behavior: HitTestBehavior.translucent,
              )
            ],
          ),
        ),
      ),
    );
  }
}


class MyTextWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      minimum: EdgeInsets.symmetric(horizontal: 20,vertical: 100),
      child: Stack(
        // mainAxisAlignment: MainAxisAlignment.end,
        children: [
          GestureDetector(
            behavior: HitTestBehavior.translucent,
            onTap: (){
              print("111111");
            },
            child: Container(
              width: 200,
              height: 200,
              color: Colors.red,
            ),
          ),
          GestureDetector(
            behavior: HitTestBehavior.translucent,
            onTap: (){
              print("222222");
            },
            child: Container(
              width: 100,
              height: 100,
              color: Colors.green,
            ),
          ),
        ],
      ),
    );
  }

}

class ListenerWithDiffrentBehavior extends StatelessWidget {
  final HitTestBehavior behavior;
  final String printInfo;
  final double width;

  const ListenerWithDiffrentBehavior(
      {super.key, required this.behavior, required this.printInfo, required this.width});

  @override
  Widget build(BuildContext context) {
    return  Listener(
      onPointerDown: (_){
        print(printInfo);
      },
      child: Container(
        width: width,
        height: width,
        color: CommonUtil.randomColor(),
      ),
    );
  }
}


class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;
  @override
  _MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
          title: new Text(widget.title),
        ),
        body: new Center(
          child: new Column(
            children: <Widget>[
              GestureDetector(
                onTapDown: (detail){
                  print('outer click');
                },
                // onTap: () {
                //   // 调用onTap 很多次是不会发生穿透现象的
                //   print('outer click');
                // },
                child: Container(
                  width: 200,
                  height: 200,
                  color: Colors.yellow,
                  alignment: Alignment.center,
                  child: GestureDetector(
                      // onTap: () {
                      //   // 调用onTap 很多次是不会发生穿透现象的
                      //   print('inner click');
                      // },
                      onTapDown: (detail){
                        print('inner click');
                      },
                      child: Container(width: 100, height: 100, color: Colors.red)),
                ),
              )
            ],
          ),
        ));
  }
}


class MultiGestureExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: GestureDetector(
          // 将手势识别器添加到GestureDetector
          // behaviors: [PanGestureRecognizer(), ScaleGestureRecognizer()],
          onPanUpdate: (details) {
            // 处理平移手势
            print('Panning: ${details.globalPosition}');
          },
          onScaleUpdate: (details) {
            // 处理缩放手势
            print('Scaling, scale: ${details.scale}');
          },
          child: Container(
            color: Colors.blue,
            width: 200,
            height: 200,
          ),
        ),
      ),
    );
  }
}